package cn.wolfcode.mongodb.service.impl;

import cn.wolfcode.mongodb.domain.User;
import cn.wolfcode.mongodb.service.IUserService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void save(User user) {
        mongoTemplate.save(user);
    }

    @Override
    public void update(User user) {
        mongoTemplate.save(user);
    }

    @Override
    public void delete(ObjectId id) {
        User user = new User();
        user.set_id(id);
        mongoTemplate.remove(user);
    }

    @Override
    public User get(ObjectId id) {
        return mongoTemplate.findById(id,User.class);
    }

    @Override
    public List<User> list() {
        return mongoTemplate.findAll(User.class);
    }
}
